package com.starblink.mine.ui.cells

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.widget.CircleLogoView
import com.starblink.android.common.view.decoration.SpaceItemDecoration
import com.starblink.basic.ext.load
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CommUtils
import com.starblink.home.R
import com.starblink.home.databinding.ItemHomeSubStoreAddBinding
import com.starblink.home.databinding.ItemMineRouteBinding
import com.starblink.home.databinding.ItemMineTitleBinding
import com.starblink.home.databinding.ItemSubStoreAddBinding
import com.starblink.home.databinding.ItemSubStoreEmptyBinding
import com.starblink.home.databinding.ItemSubWishlistChildBinding
import com.starblink.home.databinding.LayHomeOrMineSubStoreBinding
import com.starblink.home.databinding.LayMeBrandStoreBinding
import com.starblink.mine.ui.bean.MineItemEntry
import com.starblink.mine.ui.bean.SubBottomEntry
import com.starblink.mine.ui.bean.SubStoreEntry
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.rocketreserver.fragment.ProductF
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

/**
 * 跳转的item
 */
class MyServiceItemCell(
    private val activity: FragmentActivity,
    binding: ItemMineRouteBinding
) :
    BaseBindingVH<MineItemEntry, ItemMineRouteBinding>(binding = binding) {

    override fun onBind(post: MineItemEntry, position: Int) {
        binding.clMineRouteTxt.text = post.title

        when (post.iconPath) {
//            101 -> binding.clMineRouteImg.setImageDrawable(CommUtils.getDrawable(R.mipmap.icon_5_invite))
//            102 -> binding.clMineRouteImg.setImageDrawable(CommUtils.getDrawable(R.mipmap.icon_5_creator))
            103 -> binding.clMineRouteImg.setImageDrawable(CommUtils.getDrawable(R.mipmap.icon_5_account))
            104 -> binding.clMineRouteImg.setImageDrawable(CommUtils.getDrawable(R.mipmap.icon_5_pri_setting))
            105 -> binding.clMineRouteImg.setImageDrawable(CommUtils.getDrawable(R.mipmap.icon_5_feedback))
        }

        //埋点-- Me-MyService 点击
        binding.clMineRoute.trackData(
            element = SpmElementDef.MineServiceItem,
            page = SpmPageDef.MinePage,
            onlyHit = true,
            params = mutableMapOf(
                "paramName" to post.title,
            )
        )

        binding.clMineRoute.click {
            when (post.iconPath) {
//                101 -> activity.navigation(path = RoutePage.Login.SING_IN)
//                102 -> activity.navigation(path = RoutePage.Login.SING_IN)
                103 -> {
                    if (UserDataCenter.isLogin()) {
                        activity.navigation(path = RoutePage.Login.ACCOUNT_MANAGEMENT)
                    } else {
                        activity.navigation(path = RoutePage.Login.SING_IN)
                    }
                }
//                103 -> activity.navigation(path = RoutePage.Login.SING_IN)
                104 -> {
                    activity.navigation(path = RoutePage.Login.PRIVACY_SETTINGS)
                }

                105 -> activity.navigation(path = RoutePage.Login.FEEDBACK)

            }
        }
    }
}

/**
 * 大标题
 */
class MyServiceTitleCell(
    val activity: FragmentActivity,
    binding: ItemMineTitleBinding
) :
    BaseBindingVH<String, ItemMineTitleBinding>(binding = binding) {

    override fun onBind(post: String, position: Int) {

    }

}


/**
 * 订阅布局的Add子布局
 * spmPageValue埋点页面来源
 */
class HomeSubStoreAddCell(
    binding: ItemHomeSubStoreAddBinding,
    val spmPageValue: Int? = null,
    val listener: () -> Unit
) :
    BaseBindingVH<Int, ItemHomeSubStoreAddBinding>(binding = binding) {

    override fun onBind(post: Int, position: Int) {
        //Me-My favourite stores-Add stores 点击
        spmPageValue?.let {
            binding.iSubAdd.trackData(
                page = it,
                element = SpmElementDef.HomeNewSubStoreAdd,
                onlyHit = true
            )
        }

        binding.iSubAdd.click {
            listener.invoke()
        }
    }

}

/**
 * 订阅布局的Add子布局
 * spmPageValue埋点页面来源
 */
class SubWishlistAddCell(
    binding: ItemSubStoreAddBinding,
    val spmPageValue: Int? = null,
    val listener: () -> Unit
) :
    BaseBindingVH<Int, ItemSubStoreAddBinding>(binding = binding) {

    override fun onBind(post: Int, position: Int) {
        //Me-My favourite stores-Add stores 点击
        spmPageValue?.let {
            binding.iSubAdd.trackData(
                page = it,
                element = SpmElementDef.HomeNewSubStoreAdd,
                onlyHit = true
            )
        }

        binding.iSubAdd.click {
            listener.invoke()
        }
    }

}


/**
 * Wishlist布局的父布局  mine样式
 */

class SubWishlistMineCell(
    val activity: FragmentActivity,
    binding: LayHomeOrMineSubStoreBinding,
    val subAdapter: MutableAdapter,
    val listener: () -> Unit
) :
    BaseBindingVH<SubStoreEntry, LayHomeOrMineSubStoreBinding>(binding = binding) {

    override fun onBind(post: SubStoreEntry, position: Int) {
        //订阅
        if (binding.subStoreListRecyclerView.adapter == null) {
            var subManager = LinearLayoutManager(activity)
            subManager.orientation = LinearLayoutManager.HORIZONTAL
            binding.subStoreListRecyclerView.layoutManager = subManager
            binding.subStoreListRecyclerView.setHasFixedSize(true)
            binding.subStoreListRecyclerView.itemAnimator = null
            binding.subStoreListRecyclerView.addItemDecoration(
                SpaceItemDecoration.newHorizontalDecoration(CommUtils.dip2px(5))
            )
            binding.subStoreListRecyclerView.adapter = subAdapter

            //埋点-- Me-My favourate stores区域 曝光
            binding.subStoreListRecyclerView.trackData(
                element = SpmElementDef.MineSubStoreArea,
                page = SpmPageDef.MinePage
            )
        }

        //埋点-- Me-My favourite stores-Add stores 点击
        binding.subStoreViewClick.trackData(
            element = SpmElementDef.HomeNewSubStoreAdd,
            page = SpmPageDef.MinePage,
            onlyHit = true,
        )
        binding.subStoreState.click {
            listener.invoke()
        }
    }
}

/**
 * 品牌和店铺中部布局
 */

class SubMineBottomCell(
    val activity: FragmentActivity,
    binding: LayMeBrandStoreBinding,
    val brandListener: () -> Unit,
    val storeListener: () -> Unit,
) :
    BaseBindingVH<SubBottomEntry, LayMeBrandStoreBinding>(binding = binding) {

    val defaultColorList = listOf(
        com.starblink.basic.style.R.color.gu_bg_light1,
        com.starblink.basic.style.R.color.gu_bg_light1,
        com.starblink.basic.style.R.color.gu_bg_light2,
        com.starblink.basic.style.R.color.gu_bg_light3
    )

    override fun onBind(post: SubBottomEntry, position: Int) {
        val storeBgList =
            listOf(binding.bgStore1, binding.bgStore2, binding.bgStore3, binding.bgStore4)
        val brandBgList =
            listOf(binding.bgBrand1, binding.bgBrand2, binding.bgBrand3, binding.bgBrand4)

        val ivList = listOf(binding.ivStore1, binding.ivStore2, binding.ivStore3, binding.ivStore4)
        for (i in ivList.indices) {
            ivList[i].setImageResource(defaultColorList[i])
            storeBgList[i].setBackgroundResource(com.starblink.basic.style.R.color.white)
        }

        val ivBrandList =
            listOf(binding.ivBrand1, binding.ivBrand2, binding.ivBrand3, binding.ivBrand4)
        for (i in ivBrandList.indices) {
            ivBrandList[i].setImageResource(defaultColorList[i])
            brandBgList[i].setBackgroundResource(com.starblink.basic.style.R.color.white)
        }

        val innerBrandList =
            listOf(binding.iBrand1, binding.iBrand2, binding.iBrand3, binding.iBrand4)
        val textBrandList =
            listOf(binding.tvBrand1, binding.tvBrand2, binding.tvBrand3, binding.tvBrand4)

        for (i in innerBrandList.indices) {
            innerBrandList[i].visibleOrGone(false)
        }

        val list: MutableList<MerchantWebVOF>? =
            (post.subStoreData.value ?: mutableListOf()) as MutableList<MerchantWebVOF>?

        binding.ivStore1.visibleOrGone(list?.size ?: 0 > 0)
        binding.bgStoreAdd.visibleOrGone(list?.size ?: 0 == 0)

        if (!list.isNullOrEmpty()) {
            for (i in list.indices) {
                if (ivList.size > i) {
//                    ImageUtils.loadImageFilterTransSkGray100(
//                        list[i].logo,
//                        ivList[i],
//                        null
//                    )
                    (ivList[i] as CircleLogoView).load(
                        list[i].logo,
                        list[i].merchantName ?: "",
                        hasBgColor = true
                    )
                    storeBgList[i].setBackgroundResource(com.starblink.basic.style.R.color.gu_bg_light1)
                }
            }
            binding.frontStore4.visibleOrGone(list.size > 3)
        }

        val list2: MutableList<BrandVOF>? =
            (post.subBrandData.value ?: mutableListOf()) as MutableList<BrandVOF>?

        binding.ivBrand1.visibleOrGone(list2?.size ?: 0 > 0)
        binding.bgBrandAdd.visibleOrGone(list2?.size ?: 0 == 0)
        if (!list2.isNullOrEmpty()) {
            for (i in list2.indices) {
                if (ivBrandList.size > i) {
//                    (ivBrandList[i] as CircleLogoView).load(
//                            list2[i].logo,
//                            list2[i].name ?: "",
//                            hasBgColor = true
//                        )
                    if (list2[i].logo?.isNotEmpty() == true) {
                        ImageUtils.loadImageFilterTransSkGray100(
                            list2[i].logo,
                            ivBrandList[i],
                            null, color(com.starblink.basic.style.R.color.white)
                        )
                    } else {
                        innerBrandList[i].visibleOrGone(true) {
                            textBrandList[i].text =
                                list2[i].name?.firstOrNull()?.toString()?.uppercase()
                        }
                    }
                    brandBgList[i].setBackgroundResource(com.starblink.basic.style.R.color.gu_bg_light1)
                }
            }
            binding.frontBrand4.visibleOrGone(list2.size > 3)
        }


        binding.llStore.click {
            storeListener.invoke()
        }
        binding.llBrand.click {
            brandListener.invoke()
        }
    }
}

class SubWishlistMineChildCell(
    private val activity: FragmentActivity,
    binding: ItemSubWishlistChildBinding
) :
    BaseBindingVH<ProductF, ItemSubWishlistChildBinding>(binding = binding) {

    override fun onBind(model: ProductF, position: Int) {
        handleChildVM(
            activity,
            merchant = model,
            logo = model.mainImg,
            binding = binding,
            position = position,
        ) {
        }
    }

    private fun handleChildVM(
        context: Context,
        merchant: ProductF,
        logo: String?,
        binding: ItemSubWishlistChildBinding,
        position: Int,
        lifecycleScope: CoroutineScope = MainScope(),
        refreshStatus: (status: Boolean) -> Unit
    ) {
        binding.iSubImg.load(
            logo, error = ContextCompat.getDrawable(
                itemView.context,
                com.starblink.basic.style.R.mipmap.default_img
            )
        )
        binding.front.visibleOrGone(!logo.isNullOrEmpty() && position == 4)
        //埋点-- Me-My favourite stores-店铺图标 点击和曝光
        binding.iSubImg.trackData(
            element = SpmElementDef.ELE_33032,
            page = SpmPageDef.MinePage
        )

        binding.iSubImg.click {
            navigation(path = RoutePage.Wishlist.WISHLIST_ENTRY)
        }
    }
}


//空布局暂未
class SubStoreEmptyCell(
    binding: ItemSubStoreEmptyBinding
) :
    BaseBindingVH<String, ItemSubStoreEmptyBinding>(binding = binding) {

    override fun onBind(post: String, position: Int) {
    }

}

